// Copyright (C) 2008 Taylor L. Riche <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

#include "NetRequest.hh"

using namespace requestTypes;

void
NetRequest::setFileIndex(std::string fn)
{
  uint32_t ret;
  //boost::hash<std::string> sh;
//   const char * name_string = fileName.c_str();
//   for(unsigned int i = 0; i < sizeof(*name_string); ++i) {
//     ret += *(unsigned short *)(name_string + i);
//   }
  //ret = sh(boost::lexical_cast<std::string>(srcPort) + boost::lexical_cast<std::string>(socketID) + fileName);
  //ret = sh(fileName);
  //std::cerr<< fileName << ":" << ret << std::endl;
  ret = boost::lexical_cast<int>(fileName[11]) * 10;
  ret += boost::lexical_cast<int>(fileName[12]);
  fileIndex = ret;
}

NetRequest::NetRequest(char * b, int n, socket_t s) : buffer(b),
                                              numBytes(n),
                                              socketID(s),
                                              fileName(""),
                                              close(false)
{
  char *buffer=NULL;
  char *buf=strdup(getBuffer());
  int length = 0;
  bool doneRequest = false;
  bool close = false;
  int numBytes = getNumBytes();
  do {
    char *start = buf;
    char *end;
    for (int i=0;i< numBytes;i++) {
      if (buf[i] == '\r') {
        buf[i++] = 0;
        buf[i] = 0; // Get rid of the \n
        if (length == 0) { // We're done...
          doneRequest = true;
          break;
        }
        else if(start[0]=='G' && start[1]=='E' && start[2]=='T'){
          start = strchr(start, ' ')+1;
          end = strchr(start+1, ' ');
          *end = 0;
          while (*end != 'H')
            end++;
          while (*end != '/')
            end++;
          end++;
          int major = *end-'0';
          end+=2;
          int minor = *end-'0';
          if (major != 1 || (minor > 1))
            printf("Urm, HTTP version: %d.%d we may be in trouble...\n",
                   major, minor);
          if (major == 1 && minor == 0) {
            close = true;
          }

          if (*start == '/')
            start++;

          buffer = strdup(start);
        }
        else if (start[0]=='C' && start[1]=='o' && start[2]=='n'&&
                 strstr(start, "close")) {
          close = true;
        }
        start=buf+i+1;
        length = 0;
      }
      else {
        length++;
      }
    }
  } while (!doneRequest);

  if (buffer == NULL) {
    fileName = "/sys_error.html";
  }
  else {
    for(unsigned int i = 0; i < strlen(buffer); ++i) {
      fileName += *(buffer + i);
    }
    setFileIndex(fileName);
  }

  free(buf);
  free(buffer);


}
